//
//  ThemeManager.m
//  Custom_Weibo
//
//  Created by LEO on 12/9/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "ThemeManager.h"

@implementation ThemeManager

#pragma mark - CLASS_METHODS
+ (ThemeManager *)defaultManager {
    /*  Make sure the 'manager' won't be released before the application 
     is shuted.
     */
    static ThemeManager *instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
}

#pragma mark - INITIALIZATION_METHODS
- (instancetype)init {
    self = [super init];
    
    if (self) {
        //  1)Set the default theme name for when the variable is being created.
        _themeName = kDefaultThemeName;
        
        /*  2)Make sure if there is any saved theme name at local path,read the
         saved theme name, or set a new one.
         */
        NSString *savedTheme = [[NSUserDefaults standardUserDefaults] objectForKey:kSavedThemeName];
        
        if (savedTheme.length > 0) {
            _themeName = savedTheme;
        }
        
        /*  3)Build the path for searching the theme config property list, and 
         get the data through the path.
         */
        NSString *configPath = [[NSBundle mainBundle] pathForResource:kThemeFileName
                                                               ofType:nil];
        _themeConfig = [NSDictionary dictionaryWithContentsOfFile:configPath];
        
        /*  4)Using custom method to get the theme path to dig out for further
        font path and set the font.
         */
        NSString *themePath = [self getThemePath];
        NSString *fontPath = [themePath stringByAppendingPathComponent:kConfigFileName];
        _fontConfig = [NSDictionary dictionaryWithContentsOfFile:fontPath];
    }
    return self;
}

#pragma mark SET_THEME
- (void)setThemeName:(NSString *)themeName {
    if (_themeName != themeName) {
        // 1)Decide the theme.
        _themeName = [themeName copy];
        
        /* 2)Load the corresponding color for the specialized theme from the
         config property list.
         */
        NSString *themePath = [self getThemePath];
        NSString *fontPath = [themePath stringByAppendingPathComponent:kConfigFileName];
        _fontConfig = [NSDictionary dictionaryWithContentsOfFile:fontPath];
        
        // 3)Post notification of changing theme.
        [[NSNotificationCenter defaultCenter] postNotificationName:kThemeNotification
                                                            object:nil];
        
        // 4)Save the current theme at its sandbox.
        [[NSUserDefaults standardUserDefaults] setObject:_themeName
                                                  forKey:kSavedThemeName];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

#pragma mark - GET_THEME_PATH
- (NSString *)getThemePath {
    //  1)Get the root path.
    NSString *rootPath = [[NSBundle mainBundle] resourcePath];
    
    //  2)Get the subpath for theme.
    NSString *themeSubPath = [_themeConfig objectForKey:_themeName];
    
    //  3)Combine both path for a complete path.
    NSString *path = [rootPath stringByAppendingPathComponent:themeSubPath];
    return path;
}

#pragma mark GET_IMAGE_WHEN_THEME_CHANGED
- (UIImage *)getThemeImage:(NSString *)imageName {
    if (imageName.length == 0) {
        return nil;
    }
    
    //  1)Get the theme root path first.
    NSString *path = [self getThemePath];
    
    //  2)Complete the path by adding image name.
    NSString *filePath = [path stringByAppendingPathComponent:imageName];
    
    //  3)Add the image into the view.
    UIImage *image = [UIImage imageWithContentsOfFile:filePath];
    return image;
}

#pragma mark GET_FONT_COLOR_WHEN_THEME_CHANGED
- (UIColor *)getThemeColor:(NSString *)colorName {
    if (colorName.length == 0) {
        return nil;
    }
    
    //  Set RGB color and alpha through corresponding theme.
    NSDictionary *RGBDic = [_fontConfig objectForKey:colorName];
    
    CGFloat R = [RGBDic[@"R"] floatValue];
    CGFloat G = [RGBDic[@"G"] floatValue];
    CGFloat B = [RGBDic[@"B"] floatValue];
    NSNumber *Alpha = RGBDic[@"alpha"];
    
    if (Alpha == nil) {
        //  If you don't set the alpha, default the color is opaque.
        Alpha = @1;
    }
    
    UIColor *color = kRGB(R, G, B, [Alpha floatValue]);
    return color;
}

@end
